/**
 * class represent a Single Variable of type real number.
 * @author Nadav Cohen, Natan Monitz
 *
 */
public class VariableExpression implements Expression, Variable {
	
	private char name;	//name of variable - without value

/**
 * 	Constructor Method -
 * create a new VariableExression object with the name given as the parameter.
 * @param name - a single character representing a Variable
 */
	public VariableExpression(char name)
	{
		this.name = name;
	}
	
	public char getName() {
		return name;
	}

	public double evaluate(Assignments assignments) {
		if (assignments==null)			// incase parameter given points to nowhere, throw exception
			throw new RuntimeException ("input error, assignments is not initialized");
		return assignments.valueOf(this);
	}
	
	public Expression derivative(Variable var) {
		if (var==null)				// incase parameter given points to nowhere, throw exception
			throw new RuntimeException ("input error, var is not initialized");
		Constant ans = null;		// derivative of single variable expression is always constant
		if(this.equals(var))		// in case our object variable is the one we want to derive with
			ans = new Constant(1);	// treat  our object as variable  (derive of x = 1)
		else
			ans = new Constant(0);	// else, treat  our object as constant  (derive of constant = 0)
		return ans;
			
	}
	
/**
 * compare parameter Object other to this VariableExpression. Objects Equals if:
 * - parameter other is type VariableExpression  and not null
 * - parameter other name is the same name as this object.
 *  @return true if equals, else return false.
 */
	public boolean equals(Object other)
	{
		if(other instanceof VariableExpression)
			return (this.name == ((VariableExpression)other).getName());
		else
			return false; 
	}

	public String toString(){
		return "" + name;
	}

}
